<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <!-- <el-card style="margin-top:-10px;">
        <div slot="header" class="clearfix">
          <span style="color:#329dff;font-weight:bold;font-size:18px;">新建站点</span>
        </div> -->

        <el-card style="margin-top:-2px;">
          <div slot="header" class="clearfix">
            <span style="color:#329dff;font-weight:bold;font-size:14px;">站点信息</span>
          </div>

          <el-row>
              <el-col :span="12">
                <el-form-item label="站点代码" prop="code">
                  <el-input v-model="form.code" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="站点名称" prop="name">
                  <el-input v-model="form.name" />
                </el-form-item>
              </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="负责人" prop="chargerId">
                <el-select v-model="form.chargerId" filterable placeholder="请选择负责人" style="width:100%;">
                  <el-option v-for="item in participants" :key="item.userId" :label="item.nickName" :value="item.userId"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="负责部门" prop="deptId">
                <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择负责部门" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="启用日期" prop="startTime">
                <el-date-picker clearable size="small" style="width:100%;" v-model="form.startTime" type="date" value-format="yyyy-MM-dd" placeholder="选择启动日期"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="关停日期" prop="endTime">
                <el-date-picker clearable size="small" style="width:100%;" v-model="form.endTime" type="date" value-format="yyyy-MM-dd" placeholder="选择竣工日期"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
            <el-form-item label="联系人" prop="contactPerson">
              <el-input v-model="form.contactPerson" />
            </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系方式" prop="contactPhone">
                <el-input v-model="form.contactPhone" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="站点地址" prop="address">
                <el-input v-model="form.address" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="站点描述" prop="describe">
                <el-input type="textarea" v-model="form.describe" :rows="10"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="备注说明" prop="remark">
                <el-input type="textarea" v-model="form.remark" :rows="10"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

        </el-card>

        <el-card style="margin-top:10px;">
          <div slot="header" class="clearfix">
            <span style="color:#329dff;font-weight:bold;font-size:14px;">站点附件</span>
          </div>
          <el-row>
            <el-col :span="24">
              <el-form-item prop="imgAttach">
                <image-upload-custom v-model="form.imgAttach" :limit="10000" :imageName="imageName"></image-upload-custom>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item prop="fileAttach">
                <file-upload-custom
                  :fileSize="10000"
                  :attachName="attachOtherName"
                  :fileList_show="fileList_show"
                  v-model="form.fileAttach"
                  @addlist="fileList_show_add"
                  @deletlist="fileList_show_delet"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>

      <!-- </el-card> -->
    </el-form>
    <center>
      <div slot="footer" class="dialog-footer" style="margin-top:20px;">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">重 置</el-button>
      </div>
    </center>
  </div>
</template>

<script>
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listAllUser } from "@/api/system/user";
import { editStation } from "@/api/system/station";

export default {
  name: "NewStation",
  dicts: ['sys_station_status'],
  components:{ Treeselect },
  data() {
    return {
      attachPdfName: '上传PDF',
      attachOtherName: '上传文件',
      imageName: '上传图片',
      fileList_show:[],
      participants: [],
      deptOptions: [],
      form: {},
      rules: {

      },
    };
  },
  created() {
    this.listAllUser();
    this.getTreeselect();
  },
  methods: {
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.uuid != null) {
            editStation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.$router.push({path:"/station/querystation"});
              this.reset();
            });
          } else {
            editStation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.$router.push({path:"/station/querystation"});
              this.reset();
            });
          }
        }
      });
    },
    listAllUser(){
      listAllUser().then(res =>{
        this.participants = res.data;
      })
    },
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        uuid: null,
        name: null,
        code: null,
        describe: null,
        status: null,
        startTime: null,
        endTime: null,
        chargerId: null,
        deptId: null,
        contactPerson: null,
        contactPhone: null,
        address: null,
        imgAttach: null,
        fileAttach: null,
        roleIds: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    // 文件上传接收子组件值，同步（增）
    fileList_show_add(val){
      this.fileList_show = this.fileList_show.concat(val);
    },
    // 文件上传接收子组件值，同步（减）
    fileList_show_delet(val){
      this.fileList_show.splice(val, 1);
    },
  }
};
</script>
